package com.qishanor.system.controller;

import cn.dev33.satoken.secure.SaSecureUtil;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.AES;
import com.anji.captcha.model.common.ResponseModel;
import com.anji.captcha.model.vo.CaptchaVO;
import com.anji.captcha.service.CaptchaService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qishanor.framework.constant.CacheConstant;
import com.qishanor.framework.constant.CommonConstant;
import com.qishanor.framework.util.R;
import com.qishanor.system.model.SysUser;
import com.qishanor.system.model.SysUserRole;
import com.qishanor.system.service.SysUserRoleService;
import com.qishanor.system.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Struct;
import java.util.List;

/**
 * @description: 用户登录管理
 * @author: 周振林
 * @date: 2022-04-17
 * @Copyright: 博客：http://www.zhouzhenlin.com - 沉淀、分享、成长、让自己和他人都有所收获
 **/

@Slf4j
@RestController
@RequestMapping("/api/sys/user")
public class LoginController {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private CaptchaService captchaService;


    @PostMapping("/code")
    public R getCode(@RequestBody CaptchaVO captchaVO) {
        return R.ok(captchaService.get(captchaVO));
    }
    @PostMapping("/check")
    public R checkCode(@RequestBody CaptchaVO captchaVO) {
        return R.ok(captchaService.check(captchaVO));
    }

    @PostMapping("/verify")
    public ResponseModel verify(@RequestBody CaptchaVO captchaVO) {
        return captchaService.verification(captchaVO);
    }

    @PostMapping("/login")
    public Object login(String username, String password){
//    public Object login(String username, String password,String captchaType,String code){
        //如需对password 进行解密 可以执行下面代码 块大小默认48byte
//        AES aes = new AES(Mode.CFB, Padding.NoPadding,"chaosqishanorcom".getBytes(),"chaosqishanorcom".getBytes());
//        String test=aes.encryptBase64("123456").toString();//加密
//        String password_result=aes.decryptStr(password);//解密
//        log.info(password_result);

        //验证验证码是否正确
//        if(StrUtil.isEmpty(code)){return R.failed("验证码不能为空");}
//        CaptchaVO captchaVO=new CaptchaVO();
//        captchaVO.setCaptchaVerification(code);
//        captchaVO.setCaptchaType(captchaType);
//
//        ResponseModel response =  captchaService.verification(captchaVO);
//        if(response.isSuccess() == false){
//            return R.failed(response.getRepMsg());
//        }

        //md5(md5(str) + md5(salt));
        password= SaSecureUtil.md5BySalt(password,"www.qishanor.com");
        //登录
        SysUser sysUser=sysUserService.login(username,password);
        StpUtil.login(sysUser.getId());
        //存储用户和角色信息
        List<Object> roleIds=sysUserRoleService.listObjs(Wrappers.lambdaQuery(SysUserRole.class).select(SysUserRole::getRoleId).eq(SysUserRole::getUserId,sysUser.getId()));
        StpUtil.getSession().set(CacheConstant.ROLE_DETAIL,roleIds);
        StpUtil.getSession().set(CacheConstant.USER_DETAIL,sysUser);
        Console.log(StpUtil.getTokenInfo());

        return R.ok(StpUtil.getTokenInfo());
    }

    @GetMapping("/logout")
    public Object logout(){
        StpUtil.logout();
        return R.ok();
    }


}
